﻿using JinYuan.VirtualDataLibrary;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace LargeSquareOne
{
    public partial class FrmLoginMes : Form
    {
        public FrmLoginMes()
        {
            InitializeComponent();
            this.Load += FrmLoginMes_Load;
        }

        private void FrmLoginMes_Load(object sender, EventArgs e)
        {
            CommonMethods.GetSysConfig();
            if (CommonMethods.sysConfig != null)
            {
                if (CommonMethods.sysConfig.AutoLogin)
                {
                    btn_LoginMes_Click(null, null);
                }
            }
        }

        /// <summary>
        /// Mes登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_LoginMes_Click(object sender, EventArgs e)
        {
            if (this.txt_LoginMesName.Text.Trim().Length == 0)
            {
                new FrmMsgBoxOutWithAck(2, "请填写登录用户名", "登录提示").ShowDialog();
                this.txt_LoginMesName.Focus();
                return;
            }
            if (this.txt_LoginMesPwd.Text.Trim().Length == 0)
            {
                new FrmMsgBoxOutWithAck(2, "请填写登录用密码", "登录提示").ShowDialog();
                this.txt_LoginMesPwd.Focus();
                return;
            }

            CommonMethods.mesConfig.mesUserName = this.txt_LoginMesName.Text.Trim();
            CommonMethods.mesConfig.mesPwd = this.txt_LoginMesPwd.Text.Trim();
            string Mesage = "";
            string UserCardID = CommonMethods.currentAdmin.LoginUid;
            bool Res = CommonMethods.hbgMes.LoginEnyity(CommonMethods.mesConfig.LoginMesUrl, CommonMethods.mesConfig.siteCode, CommonMethods.mesConfig.lineCode, CommonMethods.mesConfig.equipNum, CommonMethods.mesConfig.mesUserName, CommonMethods.mesConfig.mesPwd, UserCardID, ref Mesage);
            if (!Res)
            {
                new FrmMsgBoxOutWithAck(3, Mesage, "登录提示").ShowDialog();
                return;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                CommonMethods.mesConfig.IsLoginMesOK = true;
                new FrmMsgBoxOutWithAck(1, $"MES登录成功，登录用户：{CommonMethods.mesConfig.mesUserName}", "登录提示").ShowDialog();
                //登录日志
                CommonMethods.AddOPLog(false, $"MES登录成功，登录用户：{CommonMethods.mesConfig.mesUserName}");
            }
        }



        private void txt_LoginMesName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_LoginMes_Click(null, null);
            }
        }

        private void txt_LoginMesName_DoubleClick(object sender, EventArgs e)
        {
            StartKeyBoard();
        }

        #region 启动软键盘
        private void StartKeyBoard()
        {
            //打开软键盘
            try
            {
                if (!System.IO.File.Exists(Environment.SystemDirectory + "\\osk.exe"))
                {
                    MessageBox.Show("软件盘可执行文件不存在！");
                    return;
                }

                softKey = System.Diagnostics.Process.Start(Environment.SystemDirectory + "\\osk.exe");
                // 上面的语句在打开软键盘后，系统还没用立刻把软键盘的窗口创建出来了。所以下面的代码用循环来查询窗口是否创建，只有创建了窗口
                // FindWindow才能找到窗口句柄，才可以移动窗口的位置和设置窗口的大小。这里是关键。
                IntPtr intptr = IntPtr.Zero;
                while (IntPtr.Zero == intptr)
                {
                    System.Threading.Thread.Sleep(100);
                    intptr = FindWindow(null, "屏幕键盘");
                }


                // 获取屏幕尺寸
                int iActulaWidth = Screen.PrimaryScreen.Bounds.Width;
                int iActulaHeight = Screen.PrimaryScreen.Bounds.Height;


                // 设置软键盘的显示位置，底部居中
                int posX = (iActulaWidth - 1000) / 2;
                int posY = (iActulaHeight - 300);


                //设定键盘显示位置
                MoveWindow(intptr, posX, posY, 1000, 300, true);


                //设置软键盘到前端显示
                SetForegroundWindow(intptr);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        // 申明要使用的dll和api
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);


        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);


        private System.Diagnostics.Process softKey;


        #endregion

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }

        #endregion
    }
}
